/**
 * @author zhounan 
 * @date 2024年6月24日
 * @version V1.0
 */
package org.zn.quality.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zn.quality.entity.QualityExecParam;
import org.zn.quality.entity.po.QualityResultPo;
import org.zn.quality.service.QualityExecuteService;

/**
 * @author zhounan 
 * @date 2024年6月24日
 * @version V1.0
 */
@RequestMapping("quality/exec")
@RestController
public class QualityExecuteController {
	
	@Autowired
	private	QualityExecuteService qualityExecuteService;

	
	@PostMapping("/job")
	public String execByJobName(@RequestBody QualityExecParam param) throws Exception {
		
		qualityExecuteService.executeJobQuality(param.getJobName(),null,param.getParamMap());
		
		return "commit";
		
	}
	
	@PostMapping("/jobGroup")
	public Object execByJobGroupName(@RequestBody QualityExecParam param) throws Exception {
		
		qualityExecuteService.executeJobGroupQuality(param.getJobGroup(),param.getRuleType(), param.getParamMap());
		
		return "commit";
		
	}
	
	@PostMapping("/")
	public Object execById(@RequestBody QualityExecParam param) throws Exception {
		return qualityExecuteService.executeStrategy(param.getStrategyId(), param.getParamMap());
		
	}
	
	@PostMapping("/available")
	public Object execAvailable(@RequestBody QualityExecParam param) throws Exception {
		

		Thread run=new Thread(() -> {
		
				qualityExecuteService.executeAvailableStrategy(param);

		});
		
		run.start();
		
		return "commit";
		
		
	}
	
	
	@GetMapping("/result")
	public List<QualityResultPo> getResultList(Long id,String startTime,Integer warning) throws ParseException {

		DateFormat df=new SimpleDateFormat();
		Date st=df.parse(startTime);
		
		return qualityExecuteService.queryStrategyResult(id, st, warning);
		
	}
	
	
	
	@GetMapping("/result/job")
	public List<QualityResultPo> getJobResultList(Long jobId,String startTime,Integer warning) throws ParseException {

		DateFormat df=new SimpleDateFormat();
		Date st=df.parse(startTime);
		
		return qualityExecuteService.queryJobStrategyResult(jobId, st, warning);
		
	}
	
	

}
